const keySetting = [
  {
    id: "content-batch-save-shortcut",
    name: "批量采集",
    value: "Alt + 1",
    placeHolder: "输入快捷键"
  },
  {
    id: "content-capture-area-shortcut",
    name: "选择区域截图",
    value: "Alt + 2",
    placeHolder: "输入快捷键"
  },
  {
    id: "content-capture-visible-shortcut",
    name: "可视区域截图",
    value: "Alt + 3",
    placeHolder: "输入快捷键"
  },
  {
    id: "content-capture-entire-shortcut",
    name: "整页区域截图",
    value: "Alt + 4",
    placeHolder: "输入快捷键"
  },
  {
    id: "content-save-url-shortcut",
    name: "保存网址",
    value: "Alt + 5",
    placeHolder: "输入快捷键"
  }
];
/**
 * 1、获取默认快捷键
 * @date 2023-12-14
 * @returns {any}
 */
export async function getShortCutKeys() {
  const { defaultShortKeys } = await chrome.storage.local.get([
    "defaultShortKeys"
  ]);
  if (!defaultShortKeys) {
    return keySetting;
  }
  console.log("返回缓存中快捷键")
  return JSON.parse(defaultShortKeys);
}

/**
 * 2、设置默认快捷键
 * @date 2023-12-14
 * @param {any} keySetting
 * @returns {any}
 */
export function setShortCutKeys(keySettings) {
  return chrome.storage.local
    .set({
      defaultShortKeys: JSON.stringify(keySettings)
    })
    .then(() => {
      console.log("快捷键初始化成功");
    });
}

/**
 * 设置单个快捷键
 * @date 2023-12-14
 * @param {any} info
 * @returns {any}
 */
export async function setShortCutKeySetting(info) {
  const keySettings = await getShortCutKeys();
  let existSameKey = false
  // 设置前检验 快捷键的值是否重复
  keySettings.forEach((item) => {
    if(item.value == info.value){
      existSameKey = true
    }
  })
  if(existSameKey) return -1
  console.log("执行保存快捷了")
  const keySetting = keySettings.find(item => item.id === info.id);
  keySetting.value = info.value;

  await setShortCutKeys(keySettings);
}

/**
 * 根据快捷键字符串，获取要触发的事件
 * @date 2023-12-14
 * @param {any} value 快捷键字符串
 * @returns {any}
 */
export async function getShortKeySettingIdByKey(value) {
  const keySettings = await getShortCutKeys();
  const keySetting = keySettings.find(item => item.value === value);
  if (keySetting) {
    return keySetting.id;
  }
}


/**
 * 设置默认图片格式
 */

export function setDefaultImageFormatStyle(format) {
  return chrome.storage.local
  .set({
    defaultFormat: format
  })
  .then(() => {
    console.log("默认图片格式设置成功");
  });
} 

/**
 * 设置默认图片格式
 */

export async function getDefaultImageFormatStyle() {
  const { defaultFormat }= await chrome.storage.local.get([
    "defaultFormat"
  ]);
  if (!defaultFormat) {
    setDefaultImageFormatStyle('png')
    return 'png';
  }
  return defaultFormat;
} 


